package com.ruoyi.treatment.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.treatment.domain.dto.DocTreatmentSubmitDto;
import com.ruoyi.treatment.domain.dto.YaoDanDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.treatment.domain.DocTreatment;
import com.ruoyi.treatment.service.IDocTreatmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.TokenService;

/**
 * 就诊信息Controller
 *
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/treatment/treatmentMessage")
public class DocTreatmentController extends BaseController
{
    @Autowired
    private IDocTreatmentService docTreatmentService;



    /**
     * 根据处方ID获取药单信息
     */
    @ApiOperation("根据处方ID获取药单信息")
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:query')")
    @PostMapping("/getYaodan")
    public AjaxResult getYaodan(@RequestBody Map<String, String> params)
    {
        String prescriptionId = params.get("prescriptionId");
        List<YaoDanDto> yaoDan = docTreatmentService.selectYaoDanByPrescriptId(prescriptionId);
        return success(yaoDan);
    }


    /**
     * 提交就诊记录表
     */
    @ApiOperation("提交就诊记录表") // 添加这个注解
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:submitRecord')")
    @PostMapping("/submitRecord")
    public AjaxResult submitMedicine(@RequestBody DocTreatmentSubmitDto docTreatmentSubmitDto)
    {
        try {
            return toAjax(docTreatmentService.submitMedicine(docTreatmentSubmitDto));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }




    @Autowired
    private TokenService tokenService;

    /**
     * 查询就诊信息列表
     */
    @ApiOperation("就诊信息管理") // 添加这个注解
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:list')")
    @GetMapping("/list")
    public TableDataInfo list(HttpServletRequest request, DocTreatment docTreatment)
    {
        startPage();
        //从token中获取userId
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId =loginUser.getUserId();
        List<DocTreatment> list = docTreatmentService.selectDocTreatmentList(docTreatment,userId);
        return getDataTable(list);
    }

    /**
     * 导出就诊信息列表
     */
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:export')")
    @Log(title = "就诊信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DocTreatment docTreatment,HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId =loginUser.getUserId();
        List<DocTreatment> list = docTreatmentService.selectDocTreatmentList(docTreatment, userId);
        ExcelUtil<DocTreatment> util = new ExcelUtil<DocTreatment>(DocTreatment.class);
        util.exportExcel(response, list, "就诊信息数据");
    }

    /**
     * 获取就诊信息详细信息
     */
    @ApiOperation("就诊信息管理")
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:query')")
    @GetMapping(value = "/{treatId}")
    public AjaxResult getInfo(@PathVariable("treatId") Long treatId)
    {
        return success(docTreatmentService.selectDocTreatmentByTreatId(treatId));
    }

    /**
     * 新增就诊信息
     */
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:add')")
    @Log(title = "就诊信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DocTreatment docTreatment)
    {
        return toAjax(docTreatmentService.insertDocTreatment(docTreatment));
    }

    /**
     * 修改就诊信息
     */
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:edit')")
    @Log(title = "就诊信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DocTreatment docTreatment)
    {
        return toAjax(docTreatmentService.updateDocTreatment(docTreatment));
    }

    /**
     * 删除就诊信息
     */
    @PreAuthorize("@ss.hasPermi('treatment:treatmentMessage:remove')")
    @Log(title = "就诊信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{treatIds}")
    public AjaxResult remove(@PathVariable Long[] treatIds)
    {
        return toAjax(docTreatmentService.deleteDocTreatmentByTreatIds(treatIds));
    }

}
